package com.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reggie.common.R;
import com.reggie.entity.Employee;
import com.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController{
    @Autowired
    private EmployeeService employeeService;
    /**
     * 登录功能
     * */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){


        //1.将前端输入的密码数据使用md5进行加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据用户的名称进行条件查询，是否有该用户
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(queryWrapper);

        //3.没有该用户名返回错误信息
        if (one==null){
            return R.error("未找到用户");
        }

        //4.验证输入的密码与数据库中的密码是否相同
        if (!one.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //5.判断用户是否为禁用状态( 0:为禁用;1:未禁用 )
        if (one.getStatus()==0){
            return R.error("账号被禁用");
        }

        //6.将用户的id信息存储到session中
        HttpSession session = request.getSession();
        session.setAttribute("employee",one.getId());

        return R.success(one);
    }

    /**
     * 退出功能
     * */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清空session中的数据
        request.getSession().removeAttribute("employee");
        //2.返回结果
        return R.success("退出成功");
    }

    /**
     * 添加员工的信息
     * */
    @PostMapping
    public R<String> addEmployee(HttpServletRequest request,@RequestBody Employee employee){
        //定义初始密码密码并进行加密
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);
       /* employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());

        //获取当前用户的信息
        Long userId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(userId);
        employee.setUpdateUser(userId);*/

        if (employeeService.save(employee)){
            return R.success("添加成功！");
        }else {
            return R.error("添加失败");
        }
    }

    /**
     * 分页查询
     * */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name){
        Page<Employee> employeePage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
//        System.out.println(name.isEmpty());
        //使用name!=null不会产生空指针异常，而是不能用equal方法，=>  null.equal。空字符串不能调用方法
        //name.isEmpty会产生空指针异常，还是null调用了方法
            wrapper.like(name!=null,Employee::getName,name);

        employeeService.page(employeePage,wrapper);
        return R.success(employeePage);
    }

    /**
     * 编辑员工的信息
     * */
    @GetMapping("/{id}")
    public R<Employee> edit(@PathVariable Long id){
        //根据id查询用户的数据库信息，是信息进行页面的回显
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId,id);
        Employee one = employeeService.getOne(wrapper);
        return R.success(one);
    }
    /**
     * 修改员工账号的状态
     * status:1表示正常；0:表示禁用
     * @param employee 封装了id和status
     * 注：前端发送的employee对象中已经封装了修改的数据
     * */
    @PutMapping
    public R<String> updateStatus(HttpServletRequest request,@RequestBody Employee employee){
        //修改更新时间
        /*employee.setUpdateTime(LocalDateTime.now());
        //当前修改用户的id，需要通过获取session进行获取
        Long current = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(current);*/
        employeeService.updateById(employee);
        return R.success("员工信息修改成功！");
    }
}
